package cn.netswap.jeegorilla.framework.security.handler;

import cn.netswap.jeegorilla.framework.security.consts.SecurityConstants;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

@Component
public class GorillaAuthenticationSuccessHandler implements AuthenticationSuccessHandler {

    Logger log = LoggerFactory.getLogger(GorillaAuthenticationSuccessHandler.class);

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void onAuthenticationSuccess(HttpServletRequest request, HttpServletResponse response, Authentication authentication) throws IOException{
        log.info("认证成功");
        response.setContentType("application/json; charset=utf-8");
        Map<String,Object> successMap = new HashMap<String,Object>();
        successMap.put("status", SecurityConstants.SUCCESSSTATUS);
        successMap.put("msg",SecurityConstants.SUCCESSSTATUS_MSG);
        successMap.put("redirectUrl",SecurityConstants.ADMIN_INDEX);
        response.getWriter().write(objectMapper.writeValueAsString(successMap));
    }
}

